<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :formType="type" @formChange="formChangeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">关 闭</el-button>
      <multiuse-button type="primary" :click="saveHandler">保 存</multiuse-button>
    </div>
    <!-- 经销商查询 -->
    <BaseDialog :visible.sync="isDealer" title="经销商查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <OwnerSearch :rowData="formData" @close="emitCloseHandler1" />
    </BaseDialog>

    <BaseDialog :visible.sync="infoVisible" :title="'选装信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <BaseInfo :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>

    <BaseDialog :visible.sync="isProductSearch" :title="'物料选择'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <selectCar :rowData="ProductData" @close="ownerProductClose" />
    </BaseDialog>
  </div>
</template>
<script>
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { findById, saveRule ,checkProduct } from '@/api/omFinanceP/RebateManage/RebateRulesSet';
import BaseInfo from './BaseInfo';
import OwnerSearch from '../OwnerSearch';
import selectCar from './selectCar';
export default {
  mixins: [dictionary],
  props: ['type', 'rowData'],

  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      formData: {},
      infoVisible: false,
      isDealer: false,
      handleData: [],
      redata: {},
      ProductData: {},
      isSearch: false,
      isProductSearch: false,
    };
  },
  components: {
    BaseInfo,
    OwnerSearch,
    selectCar
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      if (this.type == 'edit') {
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      } else {
        // this.rowData = {};
      }
    },
    optionCodeSearch() {
      this.infoVisible = true;
    },
    dealerCodeSearch() {
      this.isDealer = true;
    },
    formChangeHandle(val) {
 
      this.saveData = val;
    },
      emitCloseHandler(val, data) {

      this.infoVisible = val;
      if (data) {
        this.handleData = data;
        //this.formList.find(x => x.fieldName == 'modelName').initialValue = data[0].modelName;
        this.formList.find(x => x.fieldName == 'optionCode').initialValue = data[0].optionCode;
        this.formList.find(x => x.fieldName == 'optionName').initialValue = data[0].optionName;
        this.formList.find(x => x.fieldName == 'packageName').initialValue = data[0].packageName;
        this.formList.find(x => x.fieldName == 'modelName').initialValue = data[0].modelName;
        // this.formList.find(x => x.fieldName == 'powerType').initialValue = parseInt(data[0].powerType);
      }
    },
    seacherFun(type) {
      if (type == 1) {
        this.isSearch = true;
      } else {
        this.isProductSearch = true;
      }
    },
    async ownerProductClose(data) {
      this.isProductSearch = false;
      if (data) {
        this.redata = data[0];
        let res = await checkProduct(this.redata);
        if (res.resultCode === 200) {
          if (res.success) {
            this.formList.find((x) => x.fieldName == 'productCode').initialValue = this.redata.productCode;
            this.formList.find((x) => x.fieldName == 'productName').initialValue = this.redata.productName;
            this.formList.find((x) => x.fieldName == 'brandName').initialValue = this.redata.brandName;
            this.formList.find((x) => x.fieldName == 'seriesName').initialValue = this.redata.seriesName;
            this.formList.find((x) => x.fieldName == 'modelName').initialValue = this.redata.modelName;
            this.formList.find((x) => x.fieldName == 'packageName').initialValue = this.redata.configName;
            this.formList.find((x) => x.fieldName == 'trimName').initialValue = this.redata.trimColorName;
            this.formList.find((x) => x.fieldName == 'colorName').initialValue = this.redata.colorName;
            this.formList.find((x) => x.fieldName == 'optionName').initialValue = this.redata.optionName;
          }
        }
      }
    },
    async saveHandler() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        let saveData = Object.assign({}, this.rowData, this.saveData);
 
        if (saveData.optionCode) {
          saveData.modelId = this.handleData.length > 0 ? this.handleData[0].modelId : this.rowData.modelId;
          
          saveData.modelCode = this.handleData.length > 0 ? this.handleData[0].modelCode : this.rowData.modelCode;
          saveData.optionId = this.handleData.length > 0 ? this.handleData[0].optionId : this.rowData.optionId;
          saveData.optionCode = this.handleData.length > 0 ? this.handleData[0].optionCode : this.rowData.optionCode;

          saveData.brandName = this.handleData.length > 0 ? this.handleData[0].brandName : this.rowData.brandName;
          saveData.seriesName = this.handleData.length > 0 ? this.handleData[0].seriesName : this.rowData.seriesName;
          saveData.packageName = this.handleData.length > 0 ? this.handleData[0].packageName : this.rowData.packageName;

          saveData.modelName = this.handleData.length > 0 ? this.handleData[0].modelName : this.rowData.modelName;
          saveData.optionName = this.handleData.length > 0 ? this.handleData[0].optionName : this.rowData.optionName;
          saveData.configName = this.handleData.length > 0 ? this.handleData[0].configName : this.rowData.configName;
          
        } else {
          saveData.modelId = '';
          saveData.modelCode = '';
          saveData.optionId = '';
          saveData.optionCode = '';
          saveData.brandName = '';
          saveData.seriesName = '';
          saveData.packageName = '';
          saveData.modelName='';
          saveData.optionName='';
          saveData.configName='';
        }
        let res = {};
        if (this.type === 'add') {
          res = await saveRule(saveData);
        }
        if (this.type === 'edit') {
          res = await saveRule(saveData);
        }
        if (res.resultCode === 200) {
          await this.closeDialog('refresh');
        }
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
   
    emitCloseHandler1(val, data) {
      this.isDealer = val;
      if (data) {
        this.formList.find(x => x.fieldName == 'dealerCode').initialValue = data[0].dealerCode;
        this.formList.find(x => x.fieldName == 'dealerName').initialValue = data[0].dealerName;
      }
    },
    createdFrom() {
      return [
         {
          type: 'INPUT',
          label: '物料编码',
          fieldName: 'productCode',
          placeholder: '请选择',
          readonly: true,
          disabled: this.rowData.ruleId != null,
          rules: [{ required: true, message: '请选择', trigger: 'blur' }],
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          },
          change: (val) => {
            this.clearFrom(val);
          },
        },
        {
          type: 'INPUT',
          label: '物料描述',
          fieldName: 'productName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '品牌',
          fieldName: 'brandName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '车系',
          fieldName: 'seriesName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '配置',
          fieldName: 'packageName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '选装',
          fieldName: 'optionName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '内饰',
          fieldName: 'trimName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '外饰',
          fieldName: 'colorName',
          placeholder: '请输入',
          disabled: true,
        },
        {
          type: 'INPUT_NUMBER',
          label: '返利上限',
          fieldName: 'rebateLimitAmount',
          placeholder: '请输入',
          precision: 2,
          disabled: moment().format('YYYY-MM-DD') >= this.rowData.startDate,
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        //       {
        //   type: 'INPUT_NUMBER',
        //   label: '台数上限',
        //   fieldName: 'maxNum',
        //   placeholder: '请输入',
        //   initialValue: '',
        //   rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        // },
         {
          type: 'SELECT',
          label: '启用状态',
          fieldName: 'status',
          placeholder: '请选择',
          initialValue: 10031001,
          itemList: this.createDictList('1003'),
          disabled: this.rowData.ruleId == null || this.rowData.status == 10031002,
          rules: [{ required: true, message: '请选择启用状态', trigger: 'change' }]
        },
        {
          type: 'DATE',
          label: '生效日期',
          fieldName: 'startDate',
          initialValue: moment().format('YYYY-MM-DD'),
          dateType: 'exactdate',
          minDateTime: moment().add(-1, 'days'),
          placeholder: '请输入',
          disabled: moment().format('YYYY-MM-DD') >= this.rowData.startDate,
          rules: [{ required: true,message: '请选择时间' }],
          change: val => {
            if (val) {
              this.formList.find(x => x.fieldName == 'endDate').initialValue = '';
              this.formList.find(x => x.fieldName == 'endDate').minDateTime = moment(new Date(val)).add(-1, 'days');
            }
          }
          
        },
        {
          type: 'DATE',
          label: '失效日期',
          fieldName: 'endDate',
          minDateTime: moment().add(-1, 'days'),
          // initialValue: moment()
          //   .endOf('year')
          //   .format('YYYY-MM-DD'),
          dateType: 'exactdate',
          placeholder: '请输入',
          disabled: this.rowData.endDate? moment().format('YYYY-MM-DD') >= this.rowData.endDate : false,
          // rules: [{ required: true, message: '请选择时间' }],
        },
      
        // {
        //   type: 'INPUT',
        //   label: '经销商代码',
        //   fieldName: 'dealerCode',
        //   placeholder: '请选择',
        //   initialValue: '',
        //   unitRender: () => {
        //     return <el-button icon="el-icon-search" onClick={() => this.dealerCodeSearch()}></el-button>;
        //   },
        //   change: val => {
        //     if (!val) {
        //       this.formList.find(x => x.fieldName == 'dealerName').initialValue = '';
        //     }
        //   }
        // },
        // {
        //   type: 'INPUT',
        //   label: '经销商名称',
        //   fieldName: 'dealerName',
        //   placeholder: '',
        //   initialValue: '',
        //   disabled: true
        // }
      ];
    }
  }
};
</script>
